/*
** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $
** Garbage Collector
** See Copyright Notice in lua.h
*/

#ifndef lgc_h
#define lgc_h

#include "lobject.h"
#include "lstate.h"

/*
** Collectable objects may have one of three colors: white, which
** means the object is not marked; gray, which means the
** object is marked, but its references may be not marked; and
** black, which means that the object and all its references are marked.
** The main invariant of the garbage collector, while marking objects,
** is that a black object can never point to a white one. Moreover,
** any gray object must be in a "gray list" (gray, grayagain, weak,
** allweak, ephemeron) so that it can be visited again before finishing
** the collection cycle. These lists have no meaning when the invariant
** is not being enforced (e.g., sweep phase).
*/

/* how much to allocate before next GC step */
#if !defined(GCSTEPSIZE)
/* ~100 small strings */
#define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
#endif

/*
** Possible states of the Garbage Collector
*/
#define GCSpropagate 0
#define GCSatomic 1
#define GCSswpallgc 2
#define GCSswpfinobj 3
#define GCSswptobefnz 4
#define GCSswpend 5
#define GCScallfin 6
#define GCSpause 7

#define issweepphase(g) (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)

/*
** macro to tell when main invariant (white objects cannot point to black
** ones) must be kept. During a collection, the sweep
** phase may break the invariant, as objects turned white may point to
** still-black objects. The invariant is restored when sweep ends and
** all objects are white again.
*/

#define keepinvariant(g) ((g)->gcstate <= GCSatomic)

/*
** some useful bit tricks
*/
#define resetbits(x, m) ((x) &= cast(lu_byte, ~(m)))
#define setbits(x, m) ((x) |= (m))
#define testbits(x, m) ((x) & (m))
#define bitmask(b) (1 << (b))
#define bit2mask(b1, b2) (bitmask(b1) | bitmask(b2))
#define l_setbit(x, b) setbits(x, bitmask(b))
#define resetbit(x, b) resetbits(x, bitmask(b))
#define testbit(x, b) testbits(x, bitmask(b))

/* Layout for bit use in 'marked' field: */
#define WHITE0BIT 0 /* object is white (type 0) */
#define WHITE1BIT 1 /* object is white (type 1) */
#define BLACKBIT 2 /* object is black */
#define FINALIZEDBIT 3 /* object has been marked for finalization */
/* bit 7 is currently used by tests (luaL_checkmemory) */

#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)

#define iswhite(x) testbits((x)->marked, WHITEBITS)
#define isblack(x) testbit((x)->marked, BLACKBIT)
#define isgray(x) /* neither white nor black */ (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))

#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)

#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
// m is current mark, m ^ WHITEBITS ==> make another white
// another white & other white ==> 0 means m is other white, so it is dead
// another white & other white ==> 1 means m is current white
// in gc phase: before atomic, every live object should be current white (other white has been collected)
// after atomic, every live has been traversal, so, they should be black (other white object is garbage)
// all the time, if some object's mark equal to other white, it must be dead
// if reversing mark has other white bit, mark should be live with current white
// if other white is 0x00, then every object would be treated as dead
#define isdeadm(ow, m) (!(((m) ^ WHITEBITS) & (ow)))
#define isdead(g, v) isdeadm(otherwhite(g), (v)->marked)

#define changewhite(x) ((x)->marked ^= WHITEBITS)
#define gray2black(x) l_setbit((x)->marked, BLACKBIT)

#define luaC_white(g) cast(lu_byte, (g)->currentwhite& WHITEBITS)

/*
** Does one step of collection when debt becomes positive. 'pre'/'pos'
** allows some adjustments to be done only when needed. macro
** 'condchangemem' is used only for heavy tests (forcing a full
** GC cycle on every opportunity)
*/
#define luaC_condGC(L, pre, pos) \
  { \
    if (G(L)->GCdebt > 0) { \
      pre; \
      luaC_step(L); \
      pos; \
    }; \
    condchangemem(L, pre, pos); \
  }

/* more often than not, 'pre'/'pos' are empty */
#define luaC_checkGC(L) luaC_condGC(L, (void)0, (void)0)

#define luaC_barrier(L, p, v) \
  ((iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? luaC_barrier_(L, obj2gco(p), gcvalue(v)) : cast_void(0))

#define luaC_barrierback(L, p, v) \
  ((iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? luaC_barrierback_(L, p) : cast_void(0))

#define luaC_objbarrier(L, p, o) ((isblack(p) && iswhite(o)) ? luaC_barrier_(L, obj2gco(p), obj2gco(o)) : cast_void(0))

#define luaC_upvalbarrier(L, uv) ((iscollectable((uv)->v) && !upisopen(uv)) ? luaC_upvalbarrier_(L, uv) : cast_void(0))

LUAI_FUNC void luaC_fix(lua_State* L, GCObject* o);
LUAI_FUNC void luaC_freeallobjects(lua_State* L);
LUAI_FUNC void luaC_step(lua_State* L);
LUAI_FUNC void luaC_onestep(lua_State* L);
LUAI_FUNC void luaC_runtilstate(lua_State* L, int statesmask);
LUAI_FUNC void luaC_fullgc(lua_State* L, int isemergency);
LUAI_FUNC GCObject* luaC_newobj(lua_State* L, int tt, size_t sz);
LUAI_FUNC void luaC_barrier_(lua_State* L, GCObject* o, GCObject* v);
LUAI_FUNC void luaC_barrierback_(lua_State* L, Table* o);
LUAI_FUNC void luaC_upvalbarrier_(lua_State* L, UpVal* uv);
LUAI_FUNC void luaC_checkfinalizer(lua_State* L, GCObject* o, Table* mt);
LUAI_FUNC void luaC_upvdeccount(lua_State* L, UpVal* uv);

#endif
